package com.itheima.integration.service.impl;

import com.itheima.integration.domain.User;
import com.itheima.integration.mapper.UserMapper;
import com.itheima.integration.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    //@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAll() {
        //1.先从缓存中查询用户
        List<User> userList = (List<User>) redisTemplate.boundValueOps("userList").get();
        //2.缓存中没有 查询数据,并放入缓存
        if (userList == null || userList.size() < 1) {
            userList = userMapper.findAll();
            redisTemplate.boundValueOps("userList").set(userList);
        }else {
            System.out.println("从缓存中读取了用户列表....");
        }
        return userList;
    }
}
